package org.micode.libs.dao.helper;

import lombok.extern.slf4j.Slf4j;
import org.micode.common.exception.VerifyFailException;
import org.micode.common.utils.EncryptUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;

@Slf4j
public class UserHelper {

    private static final int SALT_BYTES_LENGTH = 20;

    public static void checkLoginName(String name) {
        if (null == name || StringUtils.isBlank(name)) {
            throw new VerifyFailException("Login name must be non empty");
        }

        // 必须是6-16位字母、数字、下划线
        // 不能以数字开头
        String regExp = "^[^0-9][\\w_.]{4,15}$";
        if (!name.matches(regExp)) {
            throw new VerifyFailException(
                    "The login name must be 6-16 letters, numbers, dots and underscores, and cannot start with a number");
        }
    }

    public static void checkUserName(String name) {
        if (null == name || StringUtils.isBlank(name)) {
            throw new VerifyFailException("User name must be non empty");
        }
    }

    public static void checkPassword(String pwd) {
        // 必须是6-20位的字母、数字、下划线
        String regExp = "^[\\w_]{5,20}$";
        if (!pwd.matches(regExp)) {
            throw new VerifyFailException("The login password must be 6-20 letters, numbers and underscores");
        }
    }

    public static String getCipherPassword(String password, String salt) {
        return EncryptUtils.getSha256CipherText(password, salt);
    }

    public static String getNewSalt() {
        return RandomStringUtils.randomAlphanumeric(SALT_BYTES_LENGTH);
    }

    public static void main(String[] args) {
//        System.out.println(buildInviteCode(Long.MAX_VALUE));
//        System.out.println(buildInviteCode(612345678912345605L));
//        checkPassword("user0629");
//        String salt = getNewSalt();
        String salt = "SBRdkvkyFY0NJ0wCI2md";
        System.out.println(salt);
        System.out.println(getCipherPassword("admin", salt));
//        System.out.println(buildInviteCode(432345678912345601L));
    }

}
